package com.baizhi.shane.shane.service.impl;

import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.shane.dto.CommentAnswerDTO;
import com.baizhi.shane.shane.vo.CommentAnswerVO;
import com.baizhi.shane.shane.vo.PageAnswerOutVO;
import com.baizhi.shane.sys.utils.CommonUtils;
import com.baizhi.shane.sys.vo.SuccessedOutVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.shane.shane.entity.ShaneCommentAnswer;
import com.baizhi.shane.shane.service.ShaneCommentAnswerService;
import com.baizhi.shane.shane.mapper.ShaneCommentAnswerMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author wbz12
* @description 针对表【shane_comment_answer(啥呢回答评论表)】的数据库操作Service实现
* @createDate 2023-07-25 22:33:37
*/
@Slf4j
@Service
public class ShaneCommentAnswerServiceImpl extends ServiceImpl<ShaneCommentAnswerMapper, ShaneCommentAnswer>
    implements ShaneCommentAnswerService{

    @Resource
    private ShaneCommentAnswerMapper shaneCommentAnswerMapper;
    @Resource
    private CommonUtils commonUtils;

    /**
     * 获取指定回答下所有的评论
     *
     * @Author 闻柏智
     * @Date 15:06 2024/2/28
     * @Param answerId
     * @return java.util.List<com.baizhi.shane.shane.vo.CommentAnswerVO>
     **/
    @Override
    public List<CommentAnswerVO> getAllCommentByAnswerId(Long answerId) {
        // 从数据库获取数据
        List<CommentAnswerDTO> shaneCommentAnswerList = shaneCommentAnswerMapper.getAllCommentByAnswerId(answerId);

        // 返回值列表
        List<CommentAnswerVO> commentAnswerVOList = new ArrayList<>();
        /* 构造commentAnswerVOList作为返回值 */
        for (CommentAnswerDTO commentAnswerDTO : shaneCommentAnswerList) {
            /* 对一级评论做处理 */
            if (commentAnswerDTO.getCommentLevel().equals(1L)) {
                /* 复制除子评论外的所有属性 */
                CommentAnswerVO commentLevelOne = new CommentAnswerVO();
                BeanUtils.copyProperties(commentAnswerDTO, commentLevelOne);
                /* 构造children */
                List<CommentAnswerVO> children = shaneCommentAnswerList
                        .stream()
                        .filter(commentTwo -> commentTwo.getCommentLevel().equals(2L) && commentTwo.getParentId().equals(commentAnswerDTO.getId() ) )
                        .map(commentTwo -> {
                            CommentAnswerVO commentLevelTwo = new CommentAnswerVO();
                            BeanUtils.copyProperties(commentTwo, commentLevelTwo);
                            return commentLevelTwo;
                        })
                        .collect(Collectors.toList());
                commentLevelOne.setChildren(children);
                commentAnswerVOList.add(commentLevelOne);
            }
        }

        return commentAnswerVOList;
    }

    /**
     * 新增一个评论
     *
     * @Author 闻柏智
     * @Date 10:16 2024/2/28
     * @Param shaneCommentAnswer
     * @return com.baizhi.shane.sys.vo.SuccessedOutVO
     **/
    @Override
    public SuccessedOutVO insertOneComment(ShaneCommentAnswer shaneCommentAnswer) {
        boolean save = this.save(shaneCommentAnswer);
        SuccessedOutVO successedOutVO =
                commonUtils.boolToSuccessedOutVO(save, SysMapping.INSERT_COMMENT_SUCCESS, SysMapping.INSERT_COMMENT_FAIL);
        return successedOutVO;
    }
}




